<!--  -->
<template>
  <w-dialog :closeFree='true' @ok="submitOk" width="600px" title="文件授权" v-model="_value">
    <el-tree ref="treeRef" style="max-width: 600px;max-height: 400px; overflow: scroll;" :data="treeData" show-checkbox
      node-key="id" :default-expanded-keys="defaultExpandedKeys" highlight-current />
  </w-dialog>
</template>

<style lang='scss' scoped></style>
<script>

// {
//   "createItemCloudParams": [
//     {
//       "authKey": "string",
//       "authType": 0,
//       "fileKey": "string"
//     }
//   ]
// }
import { getOrganizationUser, addFileAuth } from '@/api/fileManage.js';
export default {
  components: {},
  props: {
    modelValue: {
      type: Boolean,
      default: false,
    },
    authorFileKeyList: {
      type: Array,
      default: () => {
        return []
      }
    },
    authorType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      treeData: [],
      defaultExpandedKeys: []
    };
  },
  computed: {
    _value: {
      get() {
        return this.modelValue
      },
      set(val) {
        this.$emit('update:modelValue', val)
      },
    },
  },
  watch: {},
  methods: {
    submitOk() {
      let checkNode = this.$refs.treeRef.getCheckedNodes()
      if (checkNode.length == 0) {
        this.$message.warning('请选择部门或者人员')
      } else {
        let authKeyList = checkNode.filter(i => !i.isOra).map(i => i.id)
        let obj = {
          "createItemCloudParams": [
            {
              "authKey": authKeyList.join(','),
              "authType": 1,
              "fileKey": this.authorFileKeyList.join(','),
              'isRead': this.authorType == 'isRead' ? 1: 0,
              'isDownload': this.authorType == 'isDownload' ? 1 : 0
            }
          ]
        }
        addFileAuth(obj).then(res => {
          if (res.data.success) {
            this.$message.success('授权成功')
            this._value = false
          }
        })
      }
    }
  },
  created() {
    let self = this
    let formatData = function (parent) {
      parent.forEach(i => {
        i.id = 'ora' + i.organizationKey
        i.isOra = true
        i.label = i.organizationName
        if (i.subOrgs) {
          formatData(i.subOrgs)
        }
        if (i.parentOrganizationKey == '0') {
          self.defaultExpandedKeys.push(i.id)
        }
        i.users.forEach(user_i => {
          user_i.id = user_i.userKey
          user_i.label = user_i.userName
        })
        i.children = i.subOrgs ? i.subOrgs.concat(i.users) : i.users
      });
    }
    getOrganizationUser().then(res => {
      if (res.data.success) {
        formatData(res.data.rows)
        this.treeData = res.data.rows
      }
    })
  },
  mounted() {

  },
  emits: ['update:modelValue'],
}
</script>